package Pages;

import java.util.Date;
import java.util.TreeMap;
import clientMain.*;

/**
 * Sell Item Confirm Page
 * @author Andrew Fox
 *
 */

public class SellItemConfirmPage extends Page {
	
	TreeMap<Double,StringBuffer> nextPageProbabilities=new TreeMap<Double,StringBuffer>();

	public SellItemConfirmPage(Page page){
//		super(page.url,page.lastURL,page.client);
//		this.html=page.html;
//		this.pageType=page.pageType;
		super(page.url,page.html,page.client,page.pageType);
		this.pageOpenTime=page.pageOpenTime;
		this.lastURL=page.lastURL;
		updateProbabilities();
	}
	
	/**
	 * Determines the next link to go to in the application
	 * @return the URL or HTML of the next link, depending on whether
	 * 		   the previous entry contained a form or not
	 */
	public StringBuffer makeDecision(){
		StringBuffer nextURL=runSettings.rootURL;
		
		StringBuffer nextLink=getRandomStringFromDist(nextPageProbabilities);
		
		// Think Time
		try{Thread.sleep(getThinkTime());}
		catch(InterruptedException e){
			e.printStackTrace();
		}
		if(HTML4==true){
		int end=html.indexOf("\">"+nextLink);
		int start=html.lastIndexOf("<a href=\".",end)+("<a href=\".").length();
		nextURL=nextURL.append(html.substring(start,end));
		}
		return nextURL;	
	}
	
	

	/**
	 * Updates the probabilities of which page to transition to
	 */
	public void updateProbabilities(){
		double homeProb=0;
		double browseProb=0.33;
		double sellProb=0.33;
		double myAccountProb=0.34;
		double logOutProb=0.0;
		
		double probSum=1;
		
		nextPageProbabilities.put(probSum,new StringBuffer("Home"));
		probSum-=homeProb;
		nextPageProbabilities.put(probSum,new StringBuffer("Browse"));
		probSum-=browseProb;
		nextPageProbabilities.put(probSum,new StringBuffer("Sell"));
		probSum-=sellProb;
		nextPageProbabilities.put(probSum,new StringBuffer("My Account"));
		probSum-=myAccountProb;
		nextPageProbabilities.put(probSum,new StringBuffer("Log out"));
	}
	
	/**
	 * Determines the think time before going to the next page
	 * Deducts from the think time the amount of time since the 
	 * page opened until this calculation is performed
	 * @return Think time in ms
	 */
	public int getThinkTime(){
		int thinkTime=(int)expDist(initialThinkTime);
		thinkTime+=(int)expDist(2000);
		
		if (verbose)System.out.println("User: "+client.userInfo.username+" - Think Time: "+thinkTime+" ms");
		return (int) ((thinkTime-(new Date().getTime()-pageOpenTime))/runSettings.thinkTimeSpeedUpFactor);
	}
}